package net.bsayiner.sadeceHaber.Utilities;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class NewsXmlReader {

	private static NewsXmlReader INSTANCE;

	private News news;
	private ArrayList<News> newsList;

	public static NewsXmlReader getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new NewsXmlReader();
		}
		return INSTANCE;
	}

	public ArrayList<News> readFromXml(String newsAdress) {
		newsList = new ArrayList<News>();
		URL xmlWebAdress = null;
		try {
			xmlWebAdress = new URL(newsAdress);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		SAXParserFactory parserFactory = SAXParserFactory.newInstance();
		SAXParser saxParser = null;
		try {
			saxParser = parserFactory.newSAXParser();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		XMLReader xmlReader = null;
		try {
			xmlReader = saxParser.getXMLReader();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		xmlReader.setContentHandler(new NewsXmlHandler());
		try {
			xmlReader.parse(new InputSource(xmlWebAdress.openStream()));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		return newsList;
	}

	private class NewsXmlHandler extends DefaultHandler {

		private boolean item;
		private boolean title;
		private boolean description;
		private boolean category;
		private boolean link;
		private boolean pubDate;
		private boolean img;
		private boolean author;

		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			if (qName.equals("item")) {
				news = new News();
				item = true;
			} else if (qName.equals("title") && item) {
				title = true;
			} else if (qName.equals("description") && item) {
				description = true;
			} else if (qName.equals("category") && item) {
				category = true;
			} else if (qName.equals("link") && item) {
				link = true;
			} else if (qName.equals("pubDate") && item) {
				pubDate = true;
			} else if (qName.equals("img") && item) {
				img = true;
			} else if (qName.equals("author") && item) {
				author = true;
			}
		}

		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
			if (title) {
				news.setTitle(new String(ch, start, length));
				title = false;
			} else if (description) {
				news.setDescription(new String(ch, start, length));
				description = false;
			} else if (category) {
				news.setCategory(new String(ch, start, length));
				category = false;
			} else if (link) {
				news.setLink(new String(ch, start, length));
				link = false;
			} else if (pubDate) {
				news.setPubDate(new String(ch, start, length));
				pubDate = false;
			} else if (img) {
				news.setImg(new String(ch, start, length));
				img = false;
			} else if (author) {
				news.setAuthor(new String(ch, start, length));
				author = false;
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			if (qName.equals("item")) {
				newsList.add(news);
				item = false;
			}
		}
	}

}
